﻿using System.Text;
using System.Data;

namespace msBsc.WebUi.Code
{
    public class PageHtmlGet
    {
        private static System.DateTime dtclear = System.DateTime.Now;
        private static msBsc.AppLogic.Cfg.SynDictionary _htmlcach = new AppLogic.Cfg.SynDictionary(true);
        public static msBsc.AppLogic.Cfg.SynDictionary htmlcach
        {
            get
            {
                if (dtclear.AddHours(2) < System.DateTime.Now)
                {
                    _htmlcach.Clear();
                    dtclear = dtclear.AddHours(2);
                }
                return _htmlcach;
            }
        }


        public string GetPageJson(string strPageNo)
        {
            if (string.IsNullOrEmpty(strPageNo))
            {
                return "{}";
            }
            using (Epm.AppLogic.Framework.CDbAccess objdata = new Epm.AppLogic.Framework.CDbAccess())
            {
                StringBuilder sbRetJson = new StringBuilder();
                DataSet dspage = objdata.QueryBySQL(@"SELECT page_no=M.PAGE_NO
	,page_nme=M.PAGE_NME
	,page_logo_url=M.PAGE_LOGO_URL
	,page_ad_link1=M.AD_LINK1
	,page_ad_img1=M.AD_IMG1
	,page_ad_link2=M.AD_LINK2
	,page_ad_img2=M.AD_IMG2
	,page_ad_link3=M.AD_LINK3
	,page_ad_img3=M.AD_IMG3 
    ,DATA_SQL1
    ,DATA_GROUP_LIST1
    ,DATA_SQL2
    ,DATA_GROUP_LIST2
    ,DATA_SQL3
    ,DATA_GROUP_LIST3
    FROM SHP_PAGE_MALL M WHERE PAGE_NO='" + strPageNo + "'");

                if (dspage.Tables[0].Rows.Count <= 0)
                {
                    return "{}";
                }


                //开始编码
                //页面
                sbRetJson.Append("{");
                sbRetJson.Append("\"page_nme\":\"" + dspage.Tables[0].Rows[0]["page_nme"].ToString() + "\"");
                sbRetJson.Append(",\"page_logo_url\":\"" + dspage.Tables[0].Rows[0]["page_logo_url"].ToString() + "\"");
                sbRetJson.Append(",\"page_ad_link1\":\"" + dspage.Tables[0].Rows[0]["page_ad_link1"].ToString() + "\"");
                sbRetJson.Append(",\"page_ad_img1\":\"" + dspage.Tables[0].Rows[0]["page_ad_img1"].ToString() + "\"");
                sbRetJson.Append(",\"page_ad_link2\":\"" + dspage.Tables[0].Rows[0]["page_ad_link2"].ToString() + "\"");
                sbRetJson.Append(",\"page_ad_img2\":\"" + dspage.Tables[0].Rows[0]["page_ad_img2"].ToString() + "\"");
                sbRetJson.Append(",\"page_ad_link3\":\"" + dspage.Tables[0].Rows[0]["page_ad_link3"].ToString() + "\"");
                sbRetJson.Append(",\"page_ad_img3\":\"" + dspage.Tables[0].Rows[0]["page_ad_img3"].ToString() + "\"");


                //栏目
                DataSet dsCol = objdata.QueryBySQL(@"
	SELECT 
	 col_nme=C.PAGE_COL_NME
	,col_id=C.PAGE_COL_ID  
	,col_title=C.PAGE_COL_TITLE
	,col_num=C.COL_SEQ_NUM
	,col_title_img_big=C.PAGE_COL_NME_IMG_B
	,col_title_img_small=C.PAGE_COL_NME_IMG_M
	,col_ad_title1=C.AD_TITLE1
	,col_ad_img1=C.AD_IMG1
	,col_ad_link1=C.AD_LINK1
	,col_ad_title2=C.AD_TITLE2
	,col_ad_img2=C.AD_IMG2
	,col_ad_link2=C.AD_LINK2
	,col_ad_title3=C.AD_TITLE3
	,col_ad_img3=C.AD_IMG3
	,col_ad_link3=C.AD_LINK3
	,col_ad_title4=C.AD_TITLE4
	,col_ad_img4=C.AD_IMG4
	,col_ad_link4=C.AD_LINK4
	,col_ad_title5=C.AD_TITLE5
	,col_ad_img5=C.AD_IMG5
	,col_ad_link5=C.AD_LINK5
	,col_ad_title6=C.AD_TITLE6
	,col_ad_img6=C.AD_IMG6
	,col_ad_link6=C.AD_LINK6
	,mall_no=A.MALL_NO
	,shop_id=A.COMM_TENANT_ID
	,goods_label=I.ITEM_LABEL
	,goods_new_img=CASE WHEN ISNULL(I.NEW_IMG_URL,'')='' THEN A.IMG_URL ELSE I.NEW_IMG_URL END
	,goods_num=I.SEQ_NUM
	,goods_id=A.GOODS_ID
	,goods_nme=A.GOODS_NME
	,monthly_volume=A.MONTHLY_VOLUME
	,old_price=A.GOODS_OLD_PRICE
	,new_price=A.GOODS_STRIKE_PRICE
	,img_url=A.IMG_URL 
	FROM  
	SHP_PAGE_COLUMN C 
	LEFT OUTER JOIN SHP_PAGE_COLUMN_ITEM I ON C.PAGE_COL_ID=I.PAGE_COL_ID
	LEFT OUTER JOIN SHP_GOODS_INFO A   ON I.GOODS_ID=A.GOODS_ID
	WHERE
	(C.PAGE_NO='" + strPageNo + @"') 
	ORDER BY C.COL_SEQ_NUM ASC,I.SEQ_NUM ASC
"); 
                string strjsontem1 = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dsCol.Tables[0], ""
                    , 
"col_nme"
+",col_id" 
+",col_title"
+",col_num"
+",col_title_img_big"
+",col_title_img_small"
+",col_ad_title1"
+",col_ad_img1"
+",col_ad_link1"
+",col_ad_title2"
+",col_ad_img2"
+",col_ad_link2"
+",col_ad_title3"
+",col_ad_img3"
+",col_ad_link3"
+",col_ad_title4"
+",col_ad_img4"
+",col_ad_link4"
+",col_ad_title5"
+",col_ad_img5"
+",col_ad_link5"
+",col_ad_title6"
+",col_ad_img6"
+",col_ad_link6", 1, 1000, "col_num asc,col_id asc,goods_num asc"); 

                sbRetJson.Append(",\"col\":" + strjsontem1);

                //自定义1
                if (dspage.Tables[0].Rows[0]["DATA_SQL1"].ToString().Trim() != string.Empty)
                {
                    DataSet dstm1 = objdata.QueryBySQL(dspage.Tables[0].Rows[0]["DATA_SQL1"].ToString());
                    string strjsontem2 = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dstm1.Tables[0], "", dspage.Tables[0].Rows[0]["DATA_GROUP_LIST1"].ToString(), 1, 1000, "");
                    sbRetJson.Append(",\"c_data1\":" + strjsontem2);
                }
                //自定义2
                if (dspage.Tables[0].Rows[0]["DATA_SQL2"].ToString().Trim() != string.Empty)
                {
                    DataSet dstm1 = objdata.QueryBySQL(dspage.Tables[0].Rows[0]["DATA_SQL2"].ToString());
                    string strjsontem2 = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dstm1.Tables[0], "", dspage.Tables[0].Rows[0]["DATA_GROUP_LIST2"].ToString(), 1, 1000, "");
                    sbRetJson.Append(",\"c_data2\":" + strjsontem2);
                }
                //自定义3
                if (dspage.Tables[0].Rows[0]["DATA_SQL3"].ToString().Trim() != string.Empty)
                {
                    DataSet dstm1 = objdata.QueryBySQL(dspage.Tables[0].Rows[0]["DATA_SQL3"].ToString());
                    string strjsontem2 = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dstm1.Tables[0], "", dspage.Tables[0].Rows[0]["DATA_GROUP_LIST3"].ToString(), 1, 1000, "");
                    sbRetJson.Append(",\"c_data3\":" + strjsontem2);
                } 

                sbRetJson.Append("}");
                return sbRetJson.ToString();
            }

        }

    }
}